/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石数据
 */
import * as data_format from "../data/format";
/*
 * 蛋白石组件
 */
import * as power_property from "../system/power_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";

/**
 * * 野蜂 - 侦查者
 * 
 * @param {server.Entity} object - 自身
 * 
 * @param {server.Entity} target - 目标
 * 
 * @param {boolean} erupt - 是否暴击
 */
export function Detection(object: server.Entity, target: server.Entity, erupt: boolean) {
    /**
     * * 实体查询选项
     */
    const options: server.EntityQueryOptions = {
        location: object.location,
        families: ['wasp'],
        maxDistance: 32
    };
    /**
     * * 获取实体列表
     */
    const entitys = object.dimension.getEntities(options);
    // 提供增幅
    entitys.forEach(entity => power_property.AlterProperty(entity, { raise_erupt_odds: 10 }));
    /**
     * * 爆炸事件
     * 
     * @param args - 附加参数
     */
    const powerExplode = (args: data_format.ROUTE_ANNEX_ARGS) => {
        // 验证 实体状态 是否正确
        if (!object || !target || !object.isValid() || !target.isValid()) return;
        /**
         * * 过滤器参数
         */
        const options: server.EntityQueryOptions = {
            excludeTypes: ["minecraft:item", "minecraft:xp_orb", object.typeId],
            excludeFamilies: ['abyss'],
            location: args.location,
            maxDistance: 4,
            closest: 4
        };
        /**
         * * 获取实体列表
         */
        const entitys = args.dimension.getEntities(options);
        // 创建 元素伤害
        entitys.forEach(
            entity => {
                /**
                 * * 是否暴击
                 */
                const erupt = derived_tools.IsErupt(object);
                achieve_plan.RunePowerAttack.BriefCreate(object, entity, erupt);
            }
        );
        native_tools.TrySpawnParticle(args.dimension, 'constant:smoke_rune_red', args.location);
    };
    // 创建 路径
    achieve_plan.PathExecute.Create('野蜂侦查者-炮击轨迹', 1,
        {
            location_group: [object.getHeadLocation(), target.getHeadLocation()],
            particles: ['constant:track_rune_red'],
            dimension: object.dimension,
            on_done: powerExplode,
            cooldown: 1,
            speed: 1,
        }
    );
};

/**
 * * 野蜂 - 维系者
 * 
 * @param {server.Entity} object - 自身
 */
export function Support(object: server.Entity) {
    /**
     * * 实体查询选项
     */
    const options: server.EntityQueryOptions = {
        location: object.location,
        families: ['wasp'],
        maxDistance: 64,
        closest: 5
    };
    /**
     * * 获取实体列表
     */
    const entitys = object.dimension.getEntities(options);
    // 判断实体是否存在
    if (entitys.length == 0) return;
    entitys.forEach(
        entity => {
            /**
             * * 当前生命值
             */
            const health = entity.getComponent('health');
            health?.setCurrentValue(health.currentValue + 5);
        }
    )
};

/**
 * * 野蜂 - 君临者
 * 
 * @param {server.Entity} object - 自身
 * 
 * @param {server.Entity} target - 目标
 */
export function Emperor(object: server.Entity, target: server.Entity) {
    /**
     * * 充能值
     */
    const energy = object.getDynamicProperty('wasp_emperor:energy') as number ?? 0;
    /**
     * * 爆炸事件
     * 
     * @param args - 附加参数
     */
    const powerExplode = (args: data_format.ROUTE_ANNEX_ARGS) => {
        /**
         * * 过滤器参数
         */
        const options: server.EntityQueryOptions = {
            excludeTypes: ["minecraft:item", "minecraft:xp_orb", object.typeId],
            excludeFamilies: ['abyss'],
            location: args.location,
            maxDistance: 4,
            closest: 4
        };
        /**
         * * 获取实体列表
         */
        const entitys = args.dimension.getEntities(options);
        // 创建 元素伤害
        entitys.forEach(
            entity => {
                /**
                 * * 是否暴击
                 */
                const erupt = derived_tools.IsErupt(object);
                achieve_plan.RunePowerAttack.BriefCreate(object, entity, erupt);
            }
        );
        native_tools.TrySpawnParticle(args.dimension, 'constant:smoke_rune_red', args.location);
    };
    // 获得属性增幅
    power_property.AlterProperty(object, { raise_erupt_odds: 50, raise_erupt_hurt: 150 });
    // 创建 路径
    achieve_plan.PathExecute.Create('野蜂君临者-炮击轨迹', 1,
        {
            location_group: [object.getHeadLocation(), target.getHeadLocation()],
            particles: ['constant:track_rune_red'],
            dimension: object.dimension,
            on_done: powerExplode,
            cooldown: 1,
            speed: 1,
        }
    );
    object.setDynamicProperty('wasp_emperor:energy', energy + 1);
};

/**
 * * 野蜂 - 领航者
 * 
 * @param {server.Entity} object - 自身
 */
export function Guide(object: server.Entity) {
    /**
     * * 过滤器参数
     */
    const options: server.EntityQueryOptions = {
        excludeTypes: ["minecraft:item", "minecraft:xp_orb", object.typeId],
        excludeFamilies: ['abyss'],
        location: object.location,
        maxDistance: 16
    };
    /**
     * * 获取实体列表
     */
    const entitys = object.dimension.getEntities(options);
    /**
     * * 获取 当前生命值
     */
    const health = object.getComponent('health')?.currentValue ?? 10;
    /**
     * * 获取 状态数据
     */
    const state = power_property.MergeProperty(power_property.GetProperty(object), { raise_basic_attack: health });
    // 创建 元素伤害
    entitys.forEach(
        entity => {
            /**
             * * 是否暴击
             */
            const erupt = derived_tools.IsErupt(object);
            achieve_plan.RunePowerAttack.BriefCreate(object, entity, erupt, state);
        }
    );
    // 创建 粒子效果
    native_tools.TrySpawnParticle(object.dimension, 'constant:fireworks_fireball_rune_red', object.getHeadLocation());
    native_tools.TrySpawnParticle(object.dimension, 'constant:fireworks_paper_rune_red', object.getHeadLocation());
    native_tools.TrySpawnParticle(object.dimension, 'constant:smoke_rune_red', object.location);
    object.dimension.playSound('random.explode', object.location);
    server.system.runTimeout(() => object?.remove(), 5);
}